package com.hibernate.daoimp.product;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.hibernate.dao.product.ProductAtrributeDao;
import com.hibernate.pojo.ProductAtrribute;
import com.hibernate.pojo.ProductLeibie;
import com.struts.form.ProductAtrributeForm;

public class ProductAtrributeDaoImp extends HibernateDaoSupport implements ProductAtrributeDao
{
	private boolean bool=false;
	private List list=null;
	
	/**
	 * save atrribute leibie
	 */
	public boolean saveProductAtrribute(ProductAtrribute productatrribute)
	{
		bool=false;
		try
		{
			productatrribute.setCreate_time(new Date());
			productatrribute.setEdit_time(new Date());
			this.getHibernateTemplate().save(productatrribute);
			bool=true;
		}
		catch(Exception e)
		{
			bool=false;
			e.printStackTrace();
		}
		return bool;
	}
	
	public boolean updateProductAtrribute(ProductAtrributeForm productatrributeform)
	{
		try
		{
			ProductAtrribute productatrribute=(ProductAtrribute)this.getHibernateTemplate().load(ProductAtrribute.class, productatrributeform.getId());
			productatrribute.setAtrribute_name(productatrributeform.getAtrribute_name());
			productatrribute.setEdit_time(new Date());
			this.getHibernateTemplate().update(productatrribute);
			bool=true;
		}
		catch(Exception e)
		{
			bool=false;
			e.printStackTrace();
		}
		return bool;
	}
	
	/**
	 * delete product atrribute
	 */
	public boolean deleteProductAtrribute(Long id)
	{
		try
		{
			//init List
			list=new ArrayList();
			//load will delete type
			ProductAtrribute productatrribute=(ProductAtrribute)this.getHibernateTemplate().load(ProductAtrribute.class, id);
			list.add(productatrribute);
			//delete related articles
			final Long proatrribute=productatrribute.getId();
			this.getHibernateTemplate().execute(new HibernateCallback()
			{
				public Object doInHibernate(Session session)throws HibernateException, SQLException
				{
					Query query=session.createQuery("delete from ProductAndProductAtrribute where atrribute_id=:leibie");
					query.setLong("leibie",proatrribute);
					int n=query.executeUpdate();
					return n;
				}
			});
			//call recursion delete
			List collection=deleteRecursion(id);
			//delete type and child type
			this.getHibernateTemplate().deleteAll(collection);
			bool=true;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			bool=false;
		}
		return bool;
	}
	
	/**
	 * Recursion delete child type , use stack
	 * @param id
	 * @param link
	 */
	public List deleteRecursion(Long id)
	{
		ProductAtrribute productatrribute=null;
		List collection=new ArrayList();
		collection=this.getHibernateTemplate().find("from ProductAtrribute where fatherId="+id);
		if(collection.size()>0)
		{
			for(int i=0;i<collection.size();i++)
			{
				productatrribute=(ProductAtrribute)collection.get(i);
				list.add(productatrribute);
				//delete related articles
				final Long proleibie=productatrribute.getId();
				this.getHibernateTemplate().execute(new HibernateCallback()
				{
					public Object doInHibernate(Session session)throws HibernateException, SQLException
					{
						Query query=session.createQuery("delete from ProductAndProductAtrribute where atrribute_id=:leibie");
						query.setLong("leibie",proleibie);
						int n=query.executeUpdate();
						return n;
					}
				});
				//call recursion
				deleteRecursion(productatrribute.getId());
			}
				
		}
		return list;
	}
	
	/**
	 * query atrribute list
	 */
	public List findProductAtrribute(String hql)
	{
		list=this.getHibernateTemplate().find(hql);
		return list;
	}
	
	
}
